package com.zyd.shiro.business.service;

import java.io.*;
import java.util.*;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;

import com.qiniu.common.QiniuException;
import com.zyd.shiro.util.QiniuUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;



import com.zyd.shiro.persistence.dao.MallWxappHelpDao;
import com.zyd.shiro.persistence.beans.WxappHelp;
import org.springframework.web.multipart.MultipartFile;

/**
 * 服务层
 * 
 * @author
 *
 */
@Service
public class MallWxappHelpService {

	@Autowired
	private MallWxappHelpDao wxappHelpDao;
    @Autowired
	private QiniuUtil qiniuUtil;


	public List<WxappHelp> findAll() {

		return wxappHelpDao.findAll();
	}
	/**
	 * 按时间顺序查询小知识
	 */
	public List<WxappHelp> findAllHelpASC()
	{
		return wxappHelpDao.findAllHelpASC();
	}
	/**
	 * 按时间逆序查询小知识
	 */
	public List<WxappHelp> findAllHelpDESC()
	{
		return wxappHelpDao.findAllHelpDESC();
	}

	/**
	 * 按照标题
	 */
	public List<WxappHelp> findAllByTitleASC(String title)
	{
		return wxappHelpDao.findAllByTitleASC(title);
	}
	public List<WxappHelp> findAllByTitleDESC(String title)
	{
		return wxappHelpDao.findAllByTitleDESC(title);
	}
	/**
	 * 上传图片到七牛云
	 */
	public String uploadTest(MultipartFile filepath){
		try{
			String hash = qiniuUtil.putFile(filepath.getInputStream());
			for (int i = 0; i < 100; i++) {
				System.out.println(hash);
			}
			return hash;

		}catch (Exception e){
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * 从七牛云获得图片
	 */
	public String downloadTest(String filename) throws UnsupportedEncodingException {
		return qiniuUtil.getFile(filename);
	}
	/**
	 * 分页查询
	 *
	 * @param page
	 * @param size
	 * @return
	 */
	public Page<WxappHelp> findPage(int page, int size) {
		PageRequest pageRequest = new PageRequest(page-1, size);
		return wxappHelpDao.findAll(pageRequest);
	}

	private Specification<WxappHelp> where(Map searchMap) {

		return new Specification<WxappHelp>() {

			@Override
			public Predicate toPredicate(Root<WxappHelp> root,CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
				//
				if (searchMap.get("title")!=null && !"".equals(searchMap.get("title"))) {
					predicateList.add(cb.like(root.get("title").as(String.class), "%"+(String)searchMap.get("title")+"%"));
				}
				//
				if (searchMap.get("content")!=null && !"".equals(searchMap.get("content"))) {
					predicateList.add(cb.like(root.get("content").as(String.class), "%"+(String)searchMap.get("content")+"%"));
				}
				// 标题图片
				if (searchMap.get("title_image")!=null && !"".equals(searchMap.get("title_image"))) {
					predicateList.add(cb.like(root.get("title_image").as(String.class), "%"+(String)searchMap.get("title_image")+"%"));

				}
				// 内容图片
				if (searchMap.get("content_image")!=null && !"".equals(searchMap.get("content_image"))) {
					predicateList.add(cb.like(root.get("content_image").as(String.class), "%"+(String)searchMap.get("content_image")+"%"));
				}
				if (searchMap.get("type")!=null && !"".equals(searchMap.get("type"))) {
					predicateList.add(cb.equal(root.get("type").as(Integer.class), (Integer)searchMap.get("type")));
				}
				 //cb.desc(root.get("create_time").as(Date.class));

				return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));

			}
		};

	}

	public Page<WxappHelp> findSearch(String whereString,int wheretype, int page, int size) {
		Map whereMap = new HashMap<>();
		whereMap.put("title",whereString);
		whereMap.put("type",wheretype);
		Specification<WxappHelp> specification = where(whereMap);
		Sort sort = new Sort(Sort.Direction.DESC,"createTime");
		PageRequest pageRequest = new PageRequest(page-1, size,sort);

		return wxappHelpDao.findAll(specification, pageRequest);
	}
	public Page<WxappHelp> findAllSearchByType(int wheretype, int page, int size) {
		Map whereMap = new HashMap<>();
		whereMap.put("type",wheretype);
		Specification<WxappHelp> specification = where(whereMap);
		Sort sort = new Sort(Sort.Direction.DESC,"createTime");
		PageRequest pageRequest = new PageRequest(page-1, size,sort);

		return wxappHelpDao.findAll(specification, pageRequest);
	}

	public WxappHelp findOne(Integer id) {
		return wxappHelpDao.findOne(id);
	}

	public void add(WxappHelp wxappHelp) {

		wxappHelpDao.save(wxappHelp);
	}

	public void update(WxappHelp wxappHelp) {
		wxappHelpDao.save(wxappHelp);
	}

	public void delete(Integer id) {
		wxappHelpDao.delete(id);
	}

	public void deleteList(Integer[] ids) {
		for (Integer id : ids) {
			wxappHelpDao.delete(id);
		}
	}

}

